package manage.operation;

import manage.book.Book;
import manage.book.BookList;

import java.util.Scanner;

public class Delete implements IO{
    public void work(BookList bookList){
        System.out.println("删除图书");
        Scanner scan = new Scanner(System.in);

        System.out.println("请输入你要删除的图书名：");
        String name = scan.nextLine();
        int currentSize = bookList.getUsedSize();
        boolean found = false;

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                //把最后一本书移动到当前位置
                bookList.setBooks(i, bookList.getBook(currentSize - 1));
                bookList.setBooks(currentSize, null);//把最后一本书的位置清空
                bookList.setUsedSize(currentSize - 1);
                System.out.println("删除成功");
                found = true;
                break;
            }
        }

        if(!found){
            System.out.println("未找到该图书");
        }
    }
}
